package com.ae.shield.generator.builder.recipes.assembly;

import com.google.gson.JsonObject;
import com.ae.shield.common.recipe.ModRecipesType;
import net.minecraft.advancements.Advancement;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.tags.ITag;
import net.minecraft.util.ResourceLocation;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

public class ShieldUpgradeAssemblyRecipeBuilder extends AssemblyRecipeBuilder<ShieldUpgradeAssemblyRecipeBuilder>{
    private final int oldLvl;
    private final int newLvl;

    public ShieldUpgradeAssemblyRecipeBuilder(int oldLvl, int newLvl) {
        super(Items.AIR, 1);
        this.oldLvl = oldLvl;
        this.newLvl = newLvl;
    }

    public static ShieldUpgradeAssemblyRecipeBuilder recipe(int oldLvl, int newLvl) {
        return new ShieldUpgradeAssemblyRecipeBuilder(oldLvl, newLvl);
    }

    @Override
    public void build(Consumer<IFinishedRecipe> consumer, ResourceLocation id) {
        id = new ResourceLocation(id.getNamespace(), "shield_upgrade/" + id.getPath());
        super.build(consumer, id);
    }

    @Override
    public IFinishedRecipe getResult(ResourceLocation id) {
        return new Result(id, oldLvl, newLvl, result, count, group == null ? "" : group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
    }

    public static class Result extends AssemblyRecipeBuilder.Result {
        private final int oldLvl;
        private final int newLvl;

        public Result(ResourceLocation id, int oldLvl, int newLvl, Item result, int count, String group, Ingredient backing, List<Ingredient> ingredients, @Nullable Fluid fluid, @Nullable ITag<Fluid> fluidTag, int fluidCount, int energy, int tick, Advancement.Builder advancementBuilder) {
            super(id, result, count, group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
            this.newLvl = newLvl;
            this.oldLvl = oldLvl;
        }

        @Override
        public void serialize(JsonObject json) {
            json.addProperty("old_lvl", oldLvl);
            json.addProperty("new_lvl", newLvl);
            super.serialize(json);
        }

        @Override
        public IRecipeSerializer<?> getSerializer() {
            return ModRecipesType.SHIELD_UPGRADE_ASSEMBLY_SERIALIZER.get();
        }
    }
}
